﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Decembre.Models;
using System.Drawing;
using System.IO;
using System.Threading;
using DocumentFormat.OpenXml;
using ClosedXML.Excel;

namespace Decembre.Controllers
{
    public class AdminController : Controller
    {
        //
        // GET: /Admin/

        public ActionResult Index(string p)
        {
            List<Membre> mod = null;
            Membres set = new Membres(out mod);
            if (p == "new")
            {                
                ViewBag.Params = p;
                string id = Request.QueryString["id"] == null ? "" : (Request.QueryString["id"] == "1" ? Request.QueryString["id"] : "");
                ViewBag.Res = id;
                return View("New");
            }
            else if (p == "l")
            {
                List<Membre> data = new List<Membre>();
                Membres model = new Membres(out data);
                ViewBag.Params = p;
                return View("Liste",data);
            }
            else if (p == "a")
            {
                ViewBag.Params = p;
                return View("CreateArticle");
            }
            else if (p == "admin")
            {
                ViewBag.Params = p;
                return View("STK");
            }
            else
            {
                return View();
            }                
        }
        public RedirectResult UserAdmin()
        {
            Membre insert = new Membre();
            insert.NOM = Request.Form["Nom"];
            insert.PRENOM = Request.Form["Prenom"];
            insert.DATE_NAISSANCE =DateTime.Parse(Request.Form["date_naissance"]);
            insert.ADRESSE = Request.Form["adresse"];
            insert.EMAIL = Request.Form["email"] == null ? "" : Request.Form["email"];
            insert.TELEPHONE = Request.Form["telephone"] == null ? "" : Request.Form["telephone"];
            insert.DESCRIPTION = Request.Form["description"];
            insert.ANTONY = Request.Form["antony"];
            insert.DATE_INSCRIPTION = DateTime.Now;
            insert.ACTIF = true;
            Membres modele = new Membres(insert,this, false);
            if (modele.inUse)
                return Redirect("/Admin/Index?p=new&id=1");
            else
                return Redirect("/Admin/Index");
            //return View();            
        }        
        public JsonResult ImportPhotos()
        {
            string file_ = "";
            HttpPostedFileBase uploads = Request.Files[0];
            if (uploads != null)
            {
                file_ = Session.SessionID + "-" + uploads.FileName;
                string file = Server.MapPath("~/TEMP_PHOTOS/" +Session.SessionID+"-"+uploads.FileName);                 
                uploads.SaveAs(file);
                Session["file_importer"] = file;
                //modif
                Bitmap img = new Bitmap(file);
                if (img.Height > 150 && img.Width > 150)
                {
                    //img.Dispose();
                    Bitmap img_ = new Bitmap(img, new Size(198, 168));
                    img.Dispose();
                    img_.Save(file);                    
                    img_.Dispose();
                    //System.IO.File.Delete(file);
                    //Session.Remove("file_importer");
                    //return Json(new { FileName = file_, isValid = false });
                }
            }            
            return Json(new { FileName = file_ ,isValid=true});
        }
        public JsonResult ImportFile()
        {            
            //traitement fichier excel
            HttpPostedFileBase file = HttpContext.Request.Files["dataSheet"];
            //Thread.Sleep(15000);                        
            var excel = new XLWorkbook(file.InputStream);
            IXLWorksheet w;
            if (excel.TryGetWorksheet(excel.Worksheets.First().Name, out w))
            {
                var first = w.FirstRowUsed();
                string[] header=new string[first.CellsUsed().Count()];
                //assignation header
                int i = 0;
                foreach (var c in first.CellsUsed())
                {
                    header[i] = c.Value.ToString(); i++;
                }
                foreach (var r in w.RowsUsed())
                {
                    
                }
            }
            else
            {

            }            
            excel.Dispose();
            return Json(new { Status = true });
        }
    }
}
